#include "stdafx.h"
#include "bdcUtils.h"

Gdiplus::Image* bdc::LoadPng(HINSTANCE hInst, UINT resouceId)
{
	HRSRC hResource = ::FindResource(hInst, MAKEINTRESOURCE(resouceId), _T("PNG"));
	DWORD imageSize = ::SizeofResource(hInst, hResource);

	const void* pResourceData = ::LockResource(::LoadResource(hInst, hResource));
	HGLOBAL m_hBuffer  = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
	void* pBuffer = ::GlobalLock(m_hBuffer);
	if(!pBuffer){
		::GlobalFree(m_hBuffer);
		return NULL;
	}
	CopyMemory(pBuffer, pResourceData, imageSize);

	IStream* pStream = NULL;
	::CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream);

	Gdiplus::Image* image = new Gdiplus::Image(pStream);
	pStream->Release();

	::GlobalUnlock(m_hBuffer);
	::GlobalFree(m_hBuffer);

	return image;
}